package com.karenzou.elstudio.jewels.activity;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.entity.scene.Scene.ITouchArea;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.util.Log;

import com.karenzou.elstudio.jewels.IConstant;
import com.karenzou.elstudio.jewels.util.ActivityUtil;

public class MenuActivity extends BaseGameActivity 
		implements IConstant, IOnAreaTouchListener{ 

	private BitmapTextureAtlas btnTexture;
	private TextureRegion btnRegion;
	private Sprite btnEnterGame;

	@Override
	public Engine onLoadEngine() {
		return new Engine(new EngineOptions(
				true, 
				ScreenOrientation.PORTRAIT, 
				new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), 
				new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT) ) );
	}

	@Override
	public void onLoadResources() {
		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
		initSpriteResources();
	}

	private void initSpriteResources() {
		this.btnTexture = new BitmapTextureAtlas(256, 64,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		this.btnRegion = BitmapTextureAtlasTextureRegionFactory
				.createFromAsset(this.btnTexture, this,
						"button.png", 0, 0);
		
		getEngine().getTextureManager().loadTextures(btnTexture);
	}

	@Override
	public Scene onLoadScene() {
		Scene scene = new Scene();
		initScene(scene);
		initSprite(scene);
		scene.setOnAreaTouchListener(this);
		return scene;
	}

	private void initScene(final Scene scene) {
		scene.setBackground(new ColorBackground(0.09804f, 0.6274f, 0.8784f));
	}
	
	private void initSprite(final Scene scene) {
		btnEnterGame = new Sprite(
				(CAMERA_WIDTH-btnRegion.getWidth())/2, 200, btnRegion);
		scene.attachChild(btnEnterGame);
		scene.registerTouchArea(btnEnterGame);
	}

	@Override
	public void onLoadComplete() {
	}

	@Override
	public boolean onAreaTouched(TouchEvent event, ITouchArea area, 
			float pTouchAreaLocalX, float pTouchAreaLocalY) {
		
		if (event.getAction() == TouchEvent.ACTION_DOWN &&
				area.equals(btnEnterGame)) {
			ActivityUtil.toGameActivity(this);
			finishActivity();
			return true;
		}
		return false;
	}
	
	private void finishActivity(){
		finish();
	}
}